/*
 * @lc app=leetcode.cn id=3 lang=cpp
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unsigned int max = 0;
        uint8_t letters[128] = { 0 };

        int l = 0, r = 0;
        for (; r < s.size(); r++) {
            letters[s[r]]++;
            if (letters[s[r]] > 1) {
                max = max > (r - l) ? max : (r - l);

                for (; letters[s[l]] == 1; l++)
                    letters[s[l]]--;
                
                letters[s[l]]--;
                l++;
            }
        }
        
        max = max > (r - l) ? max : (r - l);

        return max;
    }
};
// @lc code=end

